<?php
if(!defined("APP_PATH")){
    define("APP_PATH", "app");
}
define("SYS_PATH", dirname(dirname(__FILE__)).DIRECTORY_SEPARATOR);
define("APP_FULL_PATH", SYS_PATH.APP_PATH.DIRECTORY_SEPARATOR);

define("CORE_PATH",SYS_PATH."core".DIRECTORY_SEPARATOR);
include_once(CORE_PATH."error.php");

//系统路径定义
define("CTRL_PATH",APP_FULL_PATH."ctrls".DIRECTORY_SEPARATOR);
define("MODEL_PATH",APP_FULL_PATH."models".DIRECTORY_SEPARATOR);
define("VIEW_PATH",APP_FULL_PATH."views".DIRECTORY_SEPARATOR);
define("LIB_PATH",SYS_PATH."lib".DIRECTORY_SEPARATOR);
define("APP_LIB_PATH",APP_FULL_PATH."lib".DIRECTORY_SEPARATOR);
define("CONFIG_PATH",APP_FULL_PATH."config".DIRECTORY_SEPARATOR);
define("SCRIPT_NAME",$_SERVER["SCRIPT_NAME"]);

//开关配置
define("LOWER_PATH", TRUE);
define("PSEUDO_STATIC", FALSE);    //伪静态.htm,.html

$scriptFolder=dirname(SCRIPT_NAME);

//静态资源目录
define("RC_URL", (strlen($scriptFolder)==1? "":$scriptFolder)."/".APP_PATH."/static/");
unset($scriptFolder);
define("STATIC_PATH", APP_FULL_PATH."static".DIRECTORY_SEPARATOR);
/**
 *返回资源文件夹中文件URL
 *@param string $file
*/
function rc($file){
    return RC_URL.$file;
}

function url($path){
    if(PSEUDO_STATIC){
        $path=$path.".html";
    }
    return Core::getEntry()."/".$path;
}

function script($path){
    $p=rc($path);
    return "<script type=\"text/javascript\" src=\"{$p}\"></script>\r\n";
}
function css($path){
    $p=rc($path);
    return "<link rel=\"stylesheet\" href=\"{$p}\"/>\r\n";
}
class Core{
    public static $delayMap=array();
    protected static $entry;
    public static function getEntry(){
        if(!isset(self::$entry)){
            $pathInfo=array_key_exists("PATH_INFO", $_SERVER)?
                trim($_SERVER["PATH_INFO"],"/"):"";
            $pathList=  empty($pathInfo)?array():explode("/", $pathInfo);

            $parts=explode("?", $_SERVER["REQUEST_URI"]);
            $parts=explode("/",trim($parts[0],"/"));
            for($i=0;$i<count($pathList);$i++){
                array_pop($parts);
            }
            self::$entry="/".implode("/", $parts);
        }
        return self::$entry;
    }
}

function delay($key,$fun){
    Core::$delayMap[$key]=$fun;
}
function doDelay($key){
    $fun=  Core::$delayMap[$key];
    $p=  func_get_args();
    $fp= array();
    $pass=true;
    foreach ($p as $arg){
        if($pass){
            $pass=false;
            continue;
        }
        $fp[]=$arg;
    }
    return call_user_func_array($fun, $fp);
}

include CORE_PATH."libmap.php";
if(!function_exists("__autoload")){
    function __autoload($className){
        $defaultPath=LIB_PATH.$className.".php";
        
        if(file_exists( $defaultPath )){
            include_once $defaultPath;
        }
        elseif (file_exists(APP_LIB_PATH.$className.".php")) {
            include_once APP_LIB_PATH.$className.".php";
        }
        elseif( array_key_exists($className,AutoLib::$classMap) ){
            include_once LIB_PATH.AutoLib::$classMap[$className];
        }
    }
    //SessionDB::load();
}
include CORE_PATH."router.php";
include CORE_PATH."control.php";
include CORE_PATH."model.php";
include CORE_PATH."view.php";